$(function(){
	//回车搜索
	$(document).keypress(function(e) {
		//回车键事件
		if(e.which == 13) {
			var searchKeyVal = $("#searchKeyVal").val();
			$("#sync_search_key").val(searchKeyVal);
			$("#searchForm").submit();
		}
	}); 

	// 上拉加载更多
	$(window).on('scroll',function(){
		if($(window).scrollTop()>=$(document).height()-$(window).height()){

			var page_count = $("#sync_page_count").val();
			var current_page = $("#sync_page_num").val();
			if(parseInt(current_page) >= parseInt(page_count)){
				//layer.msg('已经到底了',{time:500});
				return false;
			}
			var params = "requestType=sync";
			$("input[id^='sync_']").each(function(){
				if($(this).attr("pname") == "pageNum"){
					params = params + "&tmpCurrentPage="+(parseInt($(this).val())+1);
				}else{
					params = params + "&"+$(this).attr("pname")+"="+$(this).val();
				}
			});
			// 搜索潮品
			var url = rootPath + "/wx/share/search";
			$.ajax({
				type : "post",
				url : url,
				data : params,
				datatype : "json",
				success : function(jsonObject) {
					var json = $.parseJSON(jsonObject);
					$("#sync_page_num").val(json.currentPage);
					if(json.recordList){
						for(var i = 0 ; i < json.recordList.length; i++){
							var shareInfo = json.recordList[i];
							var headImg = shareInfo.wxHeadImage;
							if (headImg.indexOf('http') < 0) {
								headImg = imgRootURL + headImg + ossImgZoomWX
							}
							var shareInfo_str = "<div class='article' id=''>" +
							"<div class='box1'>" +
							"<div class='pic'>" +
							"<img src='"+ imgRootURL + shareInfo.sharedProductImagePath + ossImgZoomWX +"' onclick='toShareDetailPage("+shareInfo.id+");'>" +
							"</div>" +
							"<div class='item-info'>" +
							"<h2 onclick='toShareDetailPage("+shareInfo.id+");'>"+shareInfo.shareTitle+"</h2>" +
							"<dl>" +
							"<dt>" +
							"<i><img src='"+headImg+"'></i>" +
							"<span>"+shareInfo.userName+"</span>" +
							"</dt>" +
							"<dt>";
							if (shareInfo.isUserLike > 0) {
								shareInfo_str += "<a href='javascriopt:void(0);' class='zan current'>" +
										"<em></em>" +
										"</a>";
							}else {
								shareInfo_str += "<a href='javascriopt:void(0);' onclick='doShareInfoLike(this,"+shareInfo.id+")' class='zan'>" +
										"<em></em>" +
										"</a>";
							}
							shareInfo_str += "<span>"+shareInfo.likeQuantity+"</span>"
							"</dt>" +
							"</dl>" +
							"</div>" +
							"</div>" +
							"</div>";
//							$("#share_info_list").append($(product_str));
							var wallDiv = $("#share_info_list").find(".wall-column")[i%2];
							$(wallDiv).append($(shareInfo_str));
						}
					}
				},
				error : function() {
					layer.msg('查询分享信息失败',{time:1000});
				}
			});
		}
	});
});

/**
 * 用户点赞
 */
function doShareInfoLike(this_, shareId){
	var url = rootPath + "/wx/share/doShareInfoLike";
	$.ajax({
		type : 'POST',
		url : rootPath + "/home/isLogin",
		async : true,
		dataType : 'json',
		success : function(json) {
			console.log("是否登录："+json.code);
			if (json.code == '1') {
				$.ajax({
					type : "post",
					url : url,
					data : {shareId: shareId},
					datatype : "json",
					success : function(jsonObject) {
						if (jsonObject.resultCode == '1') {
							$(this_).addClass("current");
							$(this_).removeAttr("onclick");
							var likeNum = parseInt($(this_).closest("dt").find("span").text())+1;
							$(this_).closest("dt").find("span").text(likeNum);
						} else {
							layer.msg('点赞失败',{time:1000});
						}
					},
					error : function() {
						layer.msg('点赞失败',{time:1000});
					}
				});
			} else {
				//授权登录
				window.location.href = rootPath + "/wx/my/toBindAccountPage?goToUrl=%2Fwx%2Fshare%2Fsearch";
			}
		}
	});
}

/**
 * 跳转至详情页
 * @param id
 */
function toShareDetailPage(id){
	window.location.href = rootPath + "/wx/share/detail?id="+id;
}